<template>
  <lay-card>

    <template v-slot:body>
      <lay-row>
        <lay-col>
          <lay-table :loading="config.loading" :resize="true" :default-toolbar="true" :columns="config.columns"
            :page="config.page" @change="change" v-model:selectedKey="config.selectedKey"
            v-model:selectedKeys="config.selectedKeys" :data-source="config.data" class="project-grids">

            <template #toolbar>
              <lay-input v-model="config.search.title" size="sm" placeholder="搜索" suffix-icon="layui-icon-search"
                style="width: 20%;"></lay-input>

              <lay-button-group v-for="n in listbutton.toolbarbuttons" :key="n">
                <lay-button :size="n.size" :type="n.status" border-style="dashed" @click="Events(n, null)"> {{ n.name
                  }}</lay-button>
              </lay-button-group>
            </template>
            <template v-slot:operator="{ row }">
              <span v-for="n in listbutton.rowbuttons" :key="n">
                <lay-tooltip :content="n.name" position="bottom" :is-dark="true">
                  <button class="a-button type--button is--circle" circle @click="Events(n, row)">
                    <lay-icon :type="n.icon" :color="n.color">

                    </lay-icon>
                  </button>
                </lay-tooltip>

              </span>

            </template>
            <template #status="{ row }">
              <div>
                {{ getstatus(row) }}
              </div>

            </template>
          </lay-table>
        </lay-col>


      </lay-row>
    </template>
  </lay-card>
</template>
<script lang="ts">
export default {
  name: "mianwait"
}
</script>
<script lang="ts" setup>
import { ref, nextTick } from "vue";
import { layer } from "@layui/layer-vue";
import webosutils from 'webosutils';
import { useRoute } from 'vue-router';
import { ReleaseIcon } from '@layui/icons-vue';
import { useRouter } from 'vue-router';
import listurils from '../../utils/listutils';
import { LayerForm} from 'webos-form-design';
import axios from "axios";
import http from "../../api/http";
const router = useRouter();
const route = useRoute();
const listbutton = ref({
  rowbuttons: [] as any,
  toolbarbuttons: [] as any,
  table: [] as any,
});
const size = ref("sm");
const config = ref({
  loading: true,
  data: [],
  selectedKey: "",
  selectedKeys: [],
  page: {
    total: 0,
    limit: 10,
    current: 1,
    showRefresh: true,
    limits: [5, 10, 20, 50, 100, 200]
  },
  columns: [],
  status: [],
  api: "",
  search: {
    parentid: route.query.parentid,
    title: ""
  }
});
const getstatus = (row: any) => {

  let tag = config.value.status.find((c: any) => c.value == row.status) as any;
  if (tag) {
    return tag.label;
  } else {
    return "未知";
  }

}
listurils.getButton(route.query.appid, config, listbutton).then((res: any) => {
  //加载完成后刷新列表

  if (res.success) {
    config.value.api = res.data.api;
    if (listbutton.value.toolbarbuttons.length > 0) {
      size.value = listbutton.value.toolbarbuttons[0].size;
      let t = listbutton.value.toolbarbuttons.find((item: any) => { return item.events == 'searchEvent' });
      if (t) {
        if (t.table != null && t.table != '') {
          try {
            debugger
            let ta = JSON.parse(t.table);
            config.value.columns = ta?.columns == null ? [] : ta?.columns;
            config.value.data = ta?.data == null ? [] : ta?.data;
            config.value.page.total = ta?.total == null ? 0 : ta?.total;
            config.value.page.current = ta?.current == null ? 1 : ta?.current;
            config.value.page.limit = ta?.limit == null ? 10 : ta?.limit;
            config.value.page.limits = ta?.limits == null ? [5, 10, 20, 50, 100, 200] : ta?.limits;
            //config.value.page.showRefresh = ta?.showRefresh;
            config.value.loading = false;
            config.value.status = ta?.status == null ? [] : ta?.status;
            if (config.value.data.length == 0) {
              listurils.searchEvent(config);
            }
            return;
          } catch (e) {
            config.value.loading = false;
          }
        }
      }
    }
    listurils.searchEvent(config);
  } else {
    layer.notify({
      title: "Error",
      content: res.msg,
      icon: 2
    })
  }

});
const change = (p: any) => {

  config.value.page.current = p.current;
  config.value.page.limit = p.limit;
  listurils.searchEvent(config);
}
const Callback = () => {
  listurils.searchEvent(config);
}
const Events = (ent: any, row: any) => {
  try {
    debugger
    switch (ent.events) {
      case "editEvent":
        if (ent.ispopup == 0) {
          var query = new Object() as any;
          query.fromid = ent.formid;
          query.instanceid = row.instanceid;
          query.zhuanti = row.classid;
          query.parentid = row.id;
          query.tabname = encodeURIComponent(row.title);
          debugger
          if (ent.api != "" && ent.api != null) {
            let appid = listurils.getUrlParams(ent.api);
            if (appid) {
              query = { ...query, ...appid };
            }
          }

          let api = ent.api == "" ? "/formdesign/submitfrom" : ent.api;
          router.push({ path: api, query: query });
        } else {
          listurils.editRowEvent(LayerForm, ent, row, { fromid: ent.formid, instanceid: row.id, callback: Callback }, {});

        }
        break;
      case "searchEvent":
        if (config.value.page) {
          listurils.searchEvent(config);
        }

        break;
      case "deleteEvent": listurils.removeRowEvent(ent, row, listurils.searchEvent, config);
        break;
      case "addEvent":
        if (ent.ispopup == 0) {
          var query = new Object() as any;
          query.fromid = ent.formid;
          query.instanceid = "";
          query.zhuanti = route.query.zhuanti;
          query.tabname = encodeURIComponent(ent.title);
          router.push({ path: ent.api, query: query })

        } else {
          listurils.addEvent(LayerForm, ent, { fromid: ent.formid, instanceid: "", callback: Callback }, {});

        }
        break;
      case "SelectEvent":
        if (config.value.selectedKeys.length == 0) {
          let param = {} as any;
          if (ent.param != null && ent.param != '') {
            try {
              param = JSON.parse(ent.param);
            } catch (e) {
              layer.notify({ title: "温馨提示", content: "配置的参数存在问题！" });
              return;

            }

          }
          if (param["download"] === "download") {

            webosutils.http.post(ent.api, { id: config.value.selectedKeys.join(",") },"正在下载",{responseType: 'blob'}).then((resp: any) => {
              let url = URL.createObjectURL(resp);
              console.log(url);
              window.location.href = url;
            }

            ).catch();

        }

          return;
        } else {
          listurils.SelectEvent(ent, row, listurils.searchEvent, config);
        }

        break;
      case "previewEvent": listurils.previewEvent(LayerForm, ent, row);
        break;
      case "downEvent":
        debugger;
        if (row == null) {

          webosutils.http.post(ent.api, { id: route.query.parentid },"",{responseType: 'blob'}).then((resp: any) => {
            // let blob =  resp;
            let url = URL.createObjectURL(resp);
            window.location.href = url;
          }

          ).catch();
          // window.location.href =  + "?id=" + route.query.parentid;
        } else {
          webosutils.http.post(ent.api, { id: row.id },"",{responseType: 'blob'}).then((resp: any) => {
            let url = URL.createObjectURL(resp);
            window.location.href = url;
          }

          ).catch();
          //  window.location.href = ent.api + "?id=" + row.id;
        }

        break;
    }
  } catch (e) {
    layer.msg("按钮解析失败", { icon: 3, time: 1000 })
  }

}
</script>
<style scoped></style>